package com.music.utils.file;

import com.music.config.MinioConfig;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

public class MinioUtil extends FileUtil {
    @Resource
    private MinioConfig minioConfig;
    @Resource
    private MinioClient client;

    private static final MinioUtil minioUtil = new MinioUtil();

    public static MinioUtil getInstance() {
        return minioUtil;
    }

    /*
     * @Description 上传
     * @Author DY
     * @Date 2021/11/29
     **/
    public String upload(MultipartFile file, String fileName) throws Exception {
        if (fileName == null) {
            return null;
        }
        PutObjectArgs args = PutObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object(fileName)
                .stream(file.getInputStream(), file.getSize(), -1)
                .contentType(file.getContentType())
                .build();
        client.putObject(args);
        return "/api/point/download/" + fileName;
    }

    /*
     * @Description 删除
     * @Author DY
     * @Date 2021/11/29
     **/
    public void remove(String fileName, String type) throws Exception {
        if (fileName.contains("周杰伦"))
            return;
        RemoveObjectArgs args = RemoveObjectArgs.builder()
                .bucket(minioConfig.getBucketName())
                .object(type+"/"+fileName)
                .build();
        client.removeObject(args);
    }
}
